package com.yunhoo.zh.db.impl;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.yunhoo.sys.db.util.IbatisTemplate;
import com.yunhoo.zh.model.User;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class RegisterImpl {

    private static final String NAMESPACE = "register.";

    public static int insert(User data) {

        SqlMapClient sqlmap = IbatisTemplate.getSqlMapClient();
        try {
            sqlmap.startTransaction();

            sqlmap.insert(NAMESPACE + "insert", data);

            sqlmap.commitTransaction();
        } catch (SQLException ex) {
            Logger.getLogger(RegisterImpl.class.getName()).log(Level.SEVERE, null, ex);

            /* 插入异常，返回失败标识 */
            return -1;
        } finally {
            try {
                sqlmap.endTransaction();
            } catch (SQLException ex1) {
                Logger.getLogger(RegisterImpl.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }

        /* 保存成功，返回标识 */
        return 1;
    }

    public static int isUserNameExists(String userName) {
        String tempUserName = null;
        SqlMapClient sqlmap = IbatisTemplate.getSqlMapClient();
        try {
            sqlmap.startTransaction();

            tempUserName = isUserNameExists(sqlmap, userName);

            sqlmap.commitTransaction();
        } catch (SQLException ex) {
            Logger.getLogger(RegisterImpl.class.getName()).log(Level.SEVERE, null, ex);

            /* 异常，返回失败标识 */
            return -1;
        } finally {
            try {
                sqlmap.endTransaction();
            } catch (SQLException ex1) {
                Logger.getLogger(RegisterImpl.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }

        /* userName 不存在 */
        if (tempUserName == null) return 0;

        /* 返回标识 */
        return 1;
    }

    private static String isUserNameExists(SqlMapClient sqlmap, String userName) throws SQLException {

        return (String) sqlmap.queryForObject(NAMESPACE + "isUserNameExists", userName);
    }

    public static int isEmailExists(String email) {
        String tempEmail = null;
        SqlMapClient sqlmap = IbatisTemplate.getSqlMapClient();
        try {
            sqlmap.startTransaction();

            tempEmail =isEmailExists(sqlmap,email);

            sqlmap.commitTransaction();
        } catch (SQLException ex) {
            Logger.getLogger(RegisterImpl.class.getName()).log(Level.SEVERE, null, ex);

            /* 异常，返回失败标识 */
            return -1;
        } finally {
            try {
                sqlmap.endTransaction();
            } catch (SQLException ex1) {
                Logger.getLogger(RegisterImpl.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }

        /* userName 不存在 */
        if (tempEmail == null) return 0;

        /* 返回标识 */
        return 1;
    }

    private static String isEmailExists(SqlMapClient sqlmap, String email) throws SQLException {
        return (String) sqlmap.queryForObject(NAMESPACE + "isEmailExists", email);
    }
}
